﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;

namespace GomeeSoft.src.classes
{
    class SQLiteQuery
    {
        bool isConnected = false;
        SQLiteConnection conn;
        SQLiteCommand cmd;
        
        public SQLiteQuery()
        {
            string connectionSting = "Data Source=" + Global.GetDatabaseDirectory() + ";";
            //string connectionSting = "Data Source=" + Global.GetDatabaseDirectory() + ";Password=" + Global.DBPassword + ";";
            if (!Global.designMode)
                isConnected = DatabaseConnection(connectionSting);
        }

        private bool DatabaseConnection(string connectionString)
        {
            if (connectionString.Length > 0)
            {
                conn = new SQLiteConnection(connectionString);
                try
                {
                    conn.Open();
                }
                catch (SQLiteException e)
                {
                    Console.WriteLine(e.Message);
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool IsConnected()
        {
            if (Global.designMode)
                return false;
            return isConnected;
        }

        public void SetPassword(string password)
        {
            if (IsConnected())
            {
                conn.ChangePassword(password);
            }
        }

        public void RemovePassword()
        {
            if (IsConnected())
            {
                conn.ChangePassword("");
            }
        }

        public void SetCommand(string sqlQuery)
        {
            if (IsConnected())
            {
                cmd = new SQLiteCommand(sqlQuery, conn);
            }
        }

        public void SetParameter(string paramName, object paramValue)
        {
            if (IsConnected())
            {
                cmd.Parameters.Add(new SQLiteParameter(paramName, paramValue));
            }
        }

        public DataTable SelectDataTable()
        {
            DataTable dataTable = new DataTable();

            if (IsConnected())
            {
                SQLiteDataAdapter adapter = new SQLiteDataAdapter();
                adapter.SelectCommand = cmd;

                try
                {
                    adapter.Fill(dataTable);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            return dataTable;
        }

        public int Execute()
        {
            if (IsConnected())
            {
                return cmd.ExecuteNonQuery();
            }
            else
            {
                return -1;
            }
        }

        public void Dispose()
        {
            cmd.Dispose();
            conn.Dispose();
        }
    }
}
